package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.db.UserApi;
import com.tanhua.dubbo.api.mapper.UserMapper;
import com.tanhua.model.db.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 服务提供者
 */
@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByMobile(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public Long add(User loginUser) {
//        loginUser.setCreated(new Date());
//        loginUser.setUpdated(new Date());
        userMapper.insert(loginUser);
        return loginUser.getId();
    }

    /**
     * 修改手机号 - 3 保存
     */
    @Override
    public void updatePhone(User user) {
        userMapper.updateById(user);
    }

    /**
     * 更新用户表中的环信信息
     * @param huanxinUser
     */
    @Override
    public void update(User huanxinUser) {
        userMapper.updateById(huanxinUser);
    }

    /**
     * 通过id查询用户信息
     * @param userId
     * @return
     */
    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 根据环信id查询 用户信息
     * @param huanxinId
     * @return
     */
    @Override
    public User findByHuanxinId(String huanxinId) {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("hx_user",huanxinId);
        User user = userMapper.selectOne(query);
        return user;
    }
}
